from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from goods.models import SKU
from goods.search_indexes import SKUIndex
from orders.models import OrderInfo, OrderGoods
from users.models import User


class SKUSerializer(serializers.ModelSerializer):
    '''SKU商品序列化器类'''
    class Meta:
        model = SKU
        fields = ('id','name','price','default_image_url','comments')



class SKUIndexSerializer(HaystackSerializer):
    '''搜索结果的序列化器类'''
    object = SKUSerializer(label='商品')

    class Meta:
        # 指定索引类
        index_classes = [SKUIndex]
        fields = ('text','object')


# 嵌套序列化获取全部订单
# class MiddleSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = OrderInfo
#         fields = ('order_id', 'status', 'pay_method', 'create_time', 'total_amount', 'freight', 'skus')
#         # fields = ('order_id', 'status', 'pay_method', 'create_time', 'total_amount', 'freight')
#
#
# # 获取全部订单
# class AllOrdersSerializer(serializers.ModelSerializer):
#
#     orderinfo_set = MiddleSerializer(many=True, label='返求全部订单')
#
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'orderinfo_set')
#
#
# # 获取全部订单内商品详情
# class AllSkuSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = OrderGoods
#         fields = ('price', 'count', 'sku')
#
#
# # 获取订单内单个商品详情
# class SingleSkuSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = SKU
#         fields = ('name', 'default_image_url')


class OrderSKUSerializers(serializers.ModelSerializer):
    """
    商品序列化器
    """
    class Meta:
        model = SKU
        read_only_fields = ['id']
        fields = ['name', 'default_image_url', 'id']

class OrderGoodsSerializers(serializers.ModelSerializer):
    """
    订单商品序列化器
    """
    sku = OrderSKUSerializers()
    class Meta:
        model = OrderGoods
        fields = ['sku', 'count', 'price']

class OrderListSerializers(serializers.ModelSerializer):
    """
    订单序列化器
    """
    skus = OrderGoodsSerializers(many=True)
    class Meta:
        model = OrderInfo
        read_only_fields = ['order_id']
        fields = ['create_time', 'order_id', 'skus', 'freight', 'pay_method', 'status']
